package com.centling.smartSealForPhone.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.SealChangeDetailActivity;
import com.centling.smartSealForPhone.bean.ChangeSeal;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.view.CircleImageView;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * @author fionera
 */
public class ChangeSealListAdapter extends RecyclerView.Adapter<ChangeSealListAdapter.SealChangeViewHolder> {
    private Context mContext;
    private List<ChangeSeal> mList;
    private SimpleDateFormat simpleData = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);

    public ChangeSealListAdapter(Context context, List<ChangeSeal> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public ChangeSealListAdapter.SealChangeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChangeSealListAdapter.SealChangeViewHolder(
                LayoutInflater.from(mContext).inflate(R.layout.rv_stamp_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final SealChangeViewHolder holder, final int position) {
        ChangeSeal item = mList.get(position);
        ImageUtil.loadAvatarImage(item.getApplicantUserAvatar(),
                holder.civHead, R.drawable.iv_avatar_default);

        holder.tvName.setText(item.getApplicantUserName());
        holder.tvTime.setText(simpleData.format(item.getCreatedDate()));
        holder.tvTitle.setText("换章原因");
        holder.tvReason.setText(item.getReason());
        if ("0".equals(item.getIsValid())) {
            String status = item.getStatus();
            switch (status) {
                case "-1":
                    holder.tvStamp.setText("审核过期");
                    holder.tvStamp.setTextColor(ContextCompat
                            .getColor(mContext, R.color.change_seal_status_audit_fail));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
                case "-2":
                    holder.tvStamp.setText("授权过期");
                    holder.tvStamp.setTextColor(ContextCompat
                            .getColor(mContext, R.color.change_seal_status_audit_fail));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
                case "0":
                    holder.tvStamp.setText("审核中");
                    holder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_auditing));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
                case "1":
                    holder.tvStamp.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            listener.onClick(mList.get(position));
                        }
                    });
                    holder.tvStamp.setText("换章");
                    holder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.blue1));
                    holder.tvStamp.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
                    break;
                case "2":
                    holder.tvStamp.setText("审核失败");
                    holder.tvStamp.setTextColor(ContextCompat
                            .getColor(mContext, R.color.change_seal_status_audit_fail));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
                case "3":
                    holder.tvStamp.setText("换章结束");
                    holder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_success));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
                case "4":
                    holder.tvStamp.setText("放弃执行");
                    holder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_drop));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
                default:
                    holder.tvStamp.setText("失效");
                    holder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_invalid));
                    holder.tvStamp.setClickable(false);
                    holder.tvStamp.setBackground(null);
                    break;
            }
            if ("1".equals(item.getStatus())) {
                holder.tvStamp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.onClick(mList.get(position));
                    }
                });
                holder.tvStamp.setText("换章");
                holder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.blue1));
                holder.tvStamp.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
            }
        } else {
            holder.tvStamp.setText("换章失效");
            holder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.change_seal_status_invalid));
            holder.tvStamp.setClickable(false);
            holder.tvStamp.setBackground(null);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, SealChangeDetailActivity.class);
                String jsonString = new Gson().toJson(mList.get(position));
                intent.putExtra("changeSeal", jsonString);
                mContext.startActivity(intent);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDetailListener.onDetailClick(v, position);
            }
        });

        holder.tvStop.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnStealChangeListener {
        void onClick(ChangeSeal item);
    }

    private OnStealChangeListener listener;

    public void setOnStealChangeListener(OnStealChangeListener l) {
        this.listener = l;
    }

    public interface OnDetailListener {
        void onDetailClick(View view, int position);
    }

    private OnDetailListener onDetailListener;

    public void setOnDetailListener(OnDetailListener onDetailListener) {
        this.onDetailListener = onDetailListener;
    }

    class SealChangeViewHolder
            extends RecyclerView.ViewHolder {
        CircleImageView civHead;
        TextView tvName, tvTime, tvReason, tvStamp, tvTitle, tvStop;

        SealChangeViewHolder(View itemView) {
            super(itemView);
            civHead = itemView.findViewById(R.id.civ_stamp_list_preview);
            tvName = itemView.findViewById(R.id.tv_stamp_list_name);
            tvReason = itemView.findViewById(R.id.tv_stamp_list_reason);
            tvTime = itemView.findViewById(R.id.tv_stamp_list_time);
            tvTitle = itemView.findViewById(R.id.tv_stamp_list_reason_title);
            tvStamp = itemView.findViewById(R.id.tv_stamp_list_stamp);
            tvStop= itemView.findViewById(R.id.tv_stamp_list_stop);
        }
    }
}
